const mongoose = require('mongoose')
const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')

const statsfn = () => Promise.all([
  Image.aggregate()
    .group({
      _id: null,
      numOfImages: {$sum: 1},
      numOfLikes: {$sum: '$likes'},
      numOfVisits: {$sum: '$visit'}      
    }),
  Comment.countDocuments()
]).then(([obj1, numOfComments]) => ({
  ...obj1[0], numOfComments
}))

module.exports = statsfn